package com.shoppingmall.http;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.PathUtils;
import com.shoppingmall.BuildConfig;
import com.yanzhenjie.kalle.Kalle;
import com.yanzhenjie.kalle.KalleConfig;
import com.yanzhenjie.kalle.OkHttpConnectFactory;
import com.yanzhenjie.kalle.connect.BroadcastNetwork;
import com.yanzhenjie.kalle.cookie.DBCookieStore;
import com.yanzhenjie.kalle.simple.cache.DiskCacheStore;

import java.util.concurrent.TimeUnit;

import static com.yanzhenjie.kalle.Headers.KEY_CONTENT_TYPE;
import static com.yanzhenjie.kalle.Headers.VALUE_APPLICATION_JSON;

/**
 * 网络请求使用帮助类
 * <p> 所有使用的第三方控件都应该在业务基础包里进行二次封装。方便以后切换控件
 *
 * @author Thomas
 * @date 2019/4/17
 * @updatelog
 */
public class HttpHelper {
    public static void init(Context context) {
        Kalle.setConfig(KalleConfig.newBuilder()
                .connectFactory(OkHttpConnectFactory.newBuilder().build())
                .connectionTimeout(10, TimeUnit.MINUTES)
                .readTimeout(10, TimeUnit.MINUTES)
                .cookieStore(DBCookieStore.newBuilder(context).build())
                .cacheStore(DiskCacheStore.newBuilder(PathUtils.getExternalAppCachePath()).build())
                .network(new BroadcastNetwork(context))
                .setHeader(KEY_CONTENT_TYPE, VALUE_APPLICATION_JSON)
                .addInterceptor(new DrumInterceptor("Kalle", BuildConfig.DEBUG) {
                })
                .converter(new JsonConverter(context))
                .build());
    }


//    public static void post(String url, JSONObject data, BaseDrumCallback drumCallback) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("ver", AppUtils.getAppVersionName());
//        jsonObject.put("platform", "android");
//        jsonObject.put("token", UserHelper.getUserToken());
//        jsonObject.put("userid", UserHelper.getUserId());
//        jsonObject.put("username", UserHelper.getUserName());
//        jsonObject.put("language", LanguageHelper.getCurrentLanguage());
//        jsonObject.put("data", data);
//        Kalle.post(url)
//                .cacheMode(CacheMode.NETWORK)
//                .body(new JsonBody(EncodeUtils.base64Encode2String(jsonObject.toJSONString().getBytes())))
//                .perform(drumCallback);
//
//    }

//    public static void post(String url, String data, SimpleCallback drumCallback) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("ver", AppUtils.getAppVersionName());
//        jsonObject.put("platform", "android");
//        jsonObject.put("token", UserHelper.getUserToken());
//        jsonObject.put("userid", UserHelper.getUserId());
//        jsonObject.put("username", UserHelper.getUserName());
//        jsonObject.put("language", LanguageHelper.getCurrentLanguage());
//        jsonObject.put("data", data);
//        Kalle.post(url)
//                .cacheMode(CacheMode.NETWORK)
//                .body(new JsonBody(EncodeUtils.base64Encode2String(jsonObject.toJSONString().getBytes())))
//                .perform(drumCallback);
//
//    }


//    public static void postWithCache(String url, JSONObject data, SimpleCallback drumCallback) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("ver", AppUtils.getAppVersionName());
//        jsonObject.put("platform", "android");
//        jsonObject.put("token", UserHelper.getUserToken());
//        jsonObject.put("userid", UserHelper.getUserId());
//        jsonObject.put("username", UserHelper.getUserName());
//        jsonObject.put("language", LanguageHelper.getCurrentLanguage());
//        jsonObject.put("data", data);
//        Kalle.post(url)
//                .cacheMode(CacheMode.READ_CACHE_NO_THEN_HTTP)
//                .body(new JsonBody(EncodeUtils.base64Encode2String(jsonObject.toJSONString().getBytes())))
//                .perform(drumCallback);
//
//    }

//    public static void post(String url, BaseDrumCallback drumCallback) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("ver", AppUtils.getAppVersionName());
//        jsonObject.put("platform", "android");
//        jsonObject.put("token", UserHelper.getUserToken());
//        jsonObject.put("userid", UserHelper.getUserId());
//        jsonObject.put("username", UserHelper.getUserName());
//        jsonObject.put("language", LanguageHelper.getCurrentLanguage());
//        Kalle.post(url)
//                .cacheMode(CacheMode.NETWORK)
//                .body(new JsonBody(EncodeUtils.base64Encode2String(jsonObject.toJSONString().getBytes())))
//                .perform(drumCallback);
//
//    }
//
//    public static void postWithCache(String url, BaseDrumCallback drumCallback) {
//        postWithCache(url, null, drumCallback);
//    }


    public static void cancelRequest(Activity activity) {
        Kalle.cancel(activity.getClass());
    }

    public static void download(String url, String filePath, String fileName, DownloadCallback callback) {
        Kalle.Download.get(url).directory(filePath).fileName(fileName).perform(callback);
    }

}
